package net.wanho.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import net.wanho.client.UserClient;
import net.wanho.entity.User;
import net.wanho.util.ExceptionUtil;
import net.wanho.vo.AjaxResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.annotation.Resource;
import java.util.Map;

/**
 * Author：汤小洋
 * Date：2024-02-21 14:20
 * Description：<描述>
 */
@RestController
@RequestMapping("/users")
public class UserController {

    // @Resource
    // private RestTemplate restTemplate; // 发送http请求

    @Resource
    private UserClient userClient;


    @GetMapping("/test")
    public String consumer(){
        // 直接调用服务提供者
        // String str = restTemplate.getForObject("http://localhost:6001/users/test", String.class);

        String str = userClient.test();

        return "consumer --> " + str;
    }

    @GetMapping
    public AjaxResult list(String username){
        return userClient.list(username);
    }

    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable Integer id){
        return userClient.removeById(id);
    }

    @PostMapping
    public AjaxResult add(@RequestBody User user){
        return userClient.add(user);
    }

    @PutMapping
    public AjaxResult modify(@RequestBody User user){
        return userClient.modify(user);
    }

    @SentinelResource(value = "UserQueryResource", blockHandlerClass = ExceptionUtil.class, blockHandler = "handleException")
    @GetMapping("/query")
    public AjaxResult query(@RequestParam Map<String,Object> map){
        return userClient.query(map);
    }

    /**
     * 处理异常方法
     */
    // public AjaxResult handleException(@RequestParam Map<String,Object> map, BlockException e){
    //     return AjaxResult.error("请求过于频繁，请稍后再试！");
    // }
}
